#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>

#define     BEEP_CNT     1
#define     BEEP_NAME    "beep"
#define     BEEP_OFF          0
#define     BEEP_ON           1

struct beep_dev
{
    dev_t devid;             /* 设备号   */
    int major;               /* 主设备号 */
    int minor;               /* 次设备号 */
    struct cdev cdev;        /* 字符设备 */
    struct class *class;     /* 类      */
    struct device_node *nd;  /* 设备节点 */
    struct device *device;   /* 设备     */
    int beep_gpio;            
};

struct beep_dev beep;

static ssize_t beep_write(struct file *pfile, 
                            const char __user *buf, size_t cnt, loff_t *ppos){
    int ret = 0;
    unsigned char databuf[1];
    struct beep_dev *dev = pfile->private_data;
    ret = copy_from_user(databuf, buf, cnt);
    if(ret < 0) {
        printk("copy_from_user failed!\r\n");
        return -EINVAL;
    }
    if (databuf[0] == BEEP_ON){
        gpio_set_value(dev->beep_gpio, 0);
    }else if(databuf[0] == BEEP_OFF)
    { 
        gpio_set_value(dev->beep_gpio, 1);
    }else
    {
        printk("command err! please input 0 or 1!\r\n");
        return -EINVAL;
    }

    return ret;

}

static int beep_open(struct inode *inode, struct file *pfile){
    pfile->private_data = &beep;
    return 0;
}

int beep_release(struct inode *pinode, struct file *pfile){
    return 0;
}

static const struct file_operations beep_fops= {
    .owner = THIS_MODULE,
    .write = beep_write,
    .open = beep_open,
    .release = beep_release,
};

static int __init beep_init(void){
    int ret = 0;

    /* 1,申请设备号 */
    beep.major = 0;   /* 设备号由内核分配 */
    if(beep.major) {  /* 定义了设备号 */
        beep.devid = MKDEV(beep.major, 0);
        ret = register_chrdev_region(beep.devid, BEEP_CNT, BEEP_NAME);
    } else {            /* 没有给定设备号 */
        ret = alloc_chrdev_region(&beep.devid, 0, BEEP_CNT, BEEP_NAME);
        beep.major = MAJOR(beep.devid);
        beep.minor = MINOR(beep.devid);
    }   
    if(ret < 0) {
        goto fail_devid;
    }
    printk("beep_devid = %d\r\n", beep.devid);
    printk("beep_major = %d\r\n", beep.major);
    printk("beep_minor = %d\r\n", beep.minor);

    /* 2,添加字符设备 */
    beep.cdev.owner = THIS_MODULE;
    cdev_init(&beep.cdev, &beep_fops);
    ret = cdev_add(&beep.cdev, beep.devid, BEEP_CNT);
    if(ret < 0) 
        goto fail_cdev;
    printk("cdev_add successful!\r\n");

    /* 3,自动创建设备节点 */
    beep.class =  class_create(THIS_MODULE, BEEP_NAME);
    if (IS_ERR(beep.class)) {
        ret = PTR_ERR(beep.class);
		goto fail_class;
    }
    printk("class_create successful!\r\n");

    beep.device = device_create(beep.class, NULL, beep.devid, NULL, BEEP_NAME);
    if (IS_ERR(beep.device)) {
        ret = PTR_ERR(beep.device);
		goto fail_device;
    }
    printk("device_create successful!\r\n");

    /* 获取设备节点 */
    beep.nd = of_find_node_by_path("/beep");
    if (beep.nd == NULL) {    /* 失败 */
		ret = -EINVAL;
		goto fail_findnode;
	}
    printk("find dts node successful!\r\n");

    /* 2, 获取BEEP_所对应的GPIO */
    beep.beep_gpio = of_get_named_gpio(beep.nd, "beep-gpio", 0);
    if(beep.beep_gpio < 0) {
        printk("can't find beep gpio\r\n");
        ret = -EINVAL;
        goto fail_findnode;
    }
    printk("beep gpio num = %d\r\n", beep.beep_gpio);
    
     /* 3,申请IO */
    ret = gpio_request(beep.beep_gpio, "beep-gpio");
	if (ret) {
		printk("Failed to request the beep gpio\r\n");
		ret = -EINVAL;
        goto fail_findnode;
	}

    /* 4,使用IO，设置为输出 */
    ret = gpio_direction_output(beep.beep_gpio, 1);
	if (ret) {
		goto fail_setoutput;
	}

    /* 5操作gpio 初始值为1，关闭 */
    gpio_set_value(beep.beep_gpio, 1); 

    return 0;


fail_setoutput:
    gpio_free(beep.beep_gpio);
fail_findnode:
    device_destroy(beep.class, beep.devid);
fail_device:
    class_destroy(beep.class);
fail_class:
    cdev_del(&beep.cdev);
fail_cdev:
    unregister_chrdev_region(beep.devid, BEEP_CNT);
fail_devid:
    return ret;
}


static void __exit beep_exit(void){

    /* 关闭beep */
    gpio_set_value(beep.beep_gpio, 1);
    /* 释放IO */
    gpio_free(beep.beep_gpio);

    /* 删除字符设备 */
    cdev_del(&beep.cdev);
    printk("cdev_del successful!\r\n");

    /* 释放设备号 */
    unregister_chrdev_region(beep.devid, BEEP_CNT);
    printk("unregister chrdev successful!\r\n");

    /* 摧毁设备*/
    device_destroy(beep.class, beep.devid);
    printk("device_destroy successful!\r\n");

    /* 摧毁类 */
    class_destroy(beep.class);
    printk("class_destroy successful!\r\n");
}

module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");
